package com.base.service.impl;

import java.util.List;

import com.base.service.RuleService;
import com.common.bean.Rule;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class RuleServiceImpl extends AbstractManager implements RuleService {

	public void addRule(Rule rule) {
		String hql = "select nvl(max(ruleno),0) from Rule ";
		long count = (Long) this.getHibernateTemplate().find(hql).get(0);
		
		count += 1;
		rule.setRuleno(count);
		this.getHibernateTemplate().save(rule);
	}

	@SuppressWarnings("unchecked")
	public List<Rule> findAllRule() {
		String hql = "from Rule order by ruleno";
		return (List<Rule>)this.getHibernateTemplate().find(hql);
	}

	public Rule findRule(long ruleno) {
		return (Rule) this.getHibernateTemplate().load(Rule.class, ruleno);
	}

	public PagerModel findRulesForPager(int offset, int pagesize) {
		String hql = "from Rule order by ruleno";
		return this.searchPaginated(hql, offset, pagesize);
	}

	public void removeRule(long ruleno) {
		Rule rule = (Rule) this.getHibernateTemplate().load(Rule.class, ruleno);
		if(rule == null ) {
			throw new RuntimeException("no instance is found.");
		}
		this.getHibernateTemplate().delete(rule);
	}

	public void updateRule(Rule rule) {
		this.getHibernateTemplate().saveOrUpdate(rule);
	}

}
